<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="80px" style="width: 80%; margin-left:50px;">
      <el-row :gutter="40">
        <el-col :span="12">
          <el-form-item label="规则名称" prop="name">
            <el-input v-model="temp.name" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="规则类型" prop="type">
            <el-select v-model="temp.type" class="filter-item" placeholder="请选择规则类型" >
              <el-option
                v-for="item,index in dictList('quality_rule_type')"
                :key="index"
                :label="item.label"
                :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="规则级别" prop="level">
            <el-select v-model="temp.level" class="filter-item" placeholder="请选择规则级别" >
              <el-option
                v-for="item,index in dictList('level')"
                :key="index"
                :label="item.label"
                :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-show="temp.type==showValue">
          <el-form-item label="规则条件" prop="capitulation.whereType" >
            <!-- <el-input v-model="temp.capitulation" type="textarea" /> -->
            <el-select v-model="temp.capitulation.whereType" class="filter-item" placeholder="请选择规则级别" >
              <el-option
                v-for="item,index in dictList('sql_where_type')"
                :key="index"
                :label="item.label"
                :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="规则值" prop="capitulation.value">
            <el-input v-model="temp.capitulation.value" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="规则描述" prop="descriptions">
            <el-input v-model="temp.descriptions" type="textarea" />
          </el-form-item>
        </el-col>
   
        <el-col :span="12">
          <el-form-item label="规则状态" prop="status" >
            <!-- <el-radio v-model="temp.status" label="0">禁用</el-radio>
            <el-radio v-model="temp.status" label="1">启用</el-radio> -->
            <el-radio-group v-model="temp.status" >
            <el-radio v-for="(item,index) in dictList('is_use')" :key="item.value"  :label="item.value*1" >
              {{ item.label }}
            </el-radio>
          </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">
        {{ $t('table.cancel') }}
      </el-button>
      <el-button type="primary" :loading="loading" @click="dialogStatus==='create'?createData():updateData()">
        {{ $t('table.confirm') }}
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { createmetadataRules, updatemetadataRules, getmetadataRules } from '@/api/rules/metadataRules'
import { getDict, getDictOne } from '@/api/rules/metadataRules'

export default {
  name: 'metadataRulesForm',
  data() {
    return {
      tableOneData: [],
      tableData: [],
      rules: {
        name: [{ required: true, message: '规则名称为必填项', trigger: 'blur' }],
        type:[{ required: true, message: '规则类型为必填项', trigger: 'blur' }],
        level:[{ required: true, message: '规则级别为必填项', trigger: 'blur' }],
        'capitulation.value':[{ required: true, message: '规则值为必填项', trigger: 'blur' }],
        status:[{ required: true, message: '规则状态为必填项', trigger: 'blur' }]
      },
      temp: {
        name: undefined,//规则名称
        type: undefined,//规则类型
        level: undefined,//规则等级
        capitulation: {
          value:undefined,//规则值
          whereType:undefined //条件语句
        }
      },
      textMap: {
        update: '编辑',
        create: '新建'
      },
      loading: false,
      dialogFormVisible: false,
      dialogStatus: '',
      showValue:''
    }
  },
  created() {
    this.dictList('quality_rule_type').forEach(item => {
      if(item.label=='条件查询'){
        this.showValue=item.value
      }
    });
  },
  mounted(){
    console.log()
  },
  methods: {
    getList() {
      this.$emit('refreshList')
    },
    resetTemp() {
      this.loading = false
      this.temp = {
        id: undefined,
        name: undefined,//规则名称
        type: undefined,//规则类型
        level: undefined,//规则等级
        capitulation: {
          value:undefined,//规则值
          whereType:undefined //条件语句
        },
        descriptions: undefined,//规则描述
        status: undefined,//规则状态
        createBy: undefined,
        createDate: undefined,
        updateBy: undefined,
        updateDate: undefined,
        delFlag: undefined
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          createmetadataRules(this.temp).then(response => {
            this.loading = false
            if (response.data.code === 0) {
              this.getList()
              this.dialogFormVisible = false
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    },
    handleUpdate(id) {
      this.resetTemp()
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
      getmetadataRules(id).then(response => {
        if (response.data.code === 0) {
         
          this.temp = response.data.data
          
          console.log(this.temp.status)
        } else {
          this.dialogFormVisible = false
          this.$message.error(response.data.msg)
        }
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          updatemetadataRules(this.temp).then(response => {
            this.loading = false
            if (response.data.code === 0) {
              this.dialogFormVisible = false
              this.getList()
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    }
  }
}
</script>